# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: ddddocr_demo.py
@time: 2021/9/5 16:12
'''
import ddddocr


import requests

# 此网站有头部顺序反爬,头部顺序不对通不过 在 http://app.gjzwfw.gov.cn/jmopen/checkValiCode.do 中复制头部信息
# 验证码请求也要加 headers , 在 http://app.gjzwfw.gov.cn/jmopen/verifyCode.do?width=100&height=55&random=0.12336915521318637 复制头部信息


def img_read():
    headers ={
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'max-age=0',
        'Cookie': 'HWWAFSESID=7460211e875de10a35f; HWWAFSESTIME=1630808133147; JMOPENSESSIONID=de9009b8-6c4d-4360-9efb-121289d8b465',
        'Host': 'app.gjzwfw.gov.cn',
        'Proxy-Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3877.400 QQBrowser/10.8.4506.400',
    }
    # session = requests.Session()
    # session.headers.clear()
    # session.headers.update(headers)
    ocr = ddddocr.DdddOcr()
    res = requests.get('http://app.gjzwfw.gov.cn/jmopen/verifyCode.do?width=100&height=55&random=0.8869635663409843',headers = headers)
    print(res)
    red = ocr.classification(res.content)
    return red

'''
http://app.gjzwfw.gov.cn/jmopen/checkValiCode.do  验证码接口  参数名称{code:aaaa}
http://apis.zwfw.hunan.gov.cn/H006/custom/search/addressn?jz=6&sz=430100&qx=430121  数据

参数
jz	6
sz	430100
qx	430121

头部信息:
Host: apis.zwfw.hunan.gov.cn
Content-Length: 0
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://app.gjzwfw.gov.cn
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3877.400 QQBrowser/10.8.4506.400
Referer: http://app.gjzwfw.gov.cn/jmopen/webapp/html5/hnsbswdcx/index.html
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
'''


# data = {
# 'jz':'6',
# 'sz':'430100',
# 'qx':'430121'
# }

def ocr_img():
    url = 'http://app.gjzwfw.gov.cn/jmopen/checkValiCode.do'
    s = img_read()
    print(s)
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Content-Length': '9',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': 'HWWAFSESID=7460211e875de10a35f; HWWAFSESTIME=1630808133147; JMOPENSESSIONID=de9009b8-6c4d-4360-9efb-121289d8b465',
        'Host': 'app.gjzwfw.gov.cn',
        'Origin': 'http://app.gjzwfw.gov.cn',
        'Proxy-Connection': 'keep-alive',
        'Referer': 'http://app.gjzwfw.gov.cn/jmopen/webapp/html5/hnsbswdcx/index.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3877.400 QQBrowser/10.8.4506.400',
    }
    data = {
        'code' : s
    }
    # 用session过头部顺序反爬
    session = requests.Session()
    session.headers.clear()
    session.headers.update(headers)
    res = session.post(url,data=data,headers = headers)
    print(res.text)
ocr_img()